/**
 * @ProjectName:放射科影像调阅系统
 * @Copyright: 2014 ShangHai DJ HealthUnion Systems Co., Ltd. All Right Reserved.
 * @address: http://www.djhealthunion.com/
 * @date: 2014-08-21 上午11:28
 * @Description:
 */
package com.mida.dms.knw.util;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhuanlan 2014-08-21 上午11:28
 * @version V1.0
 */
public class DateUtils {

	public static String parseDateToStr(Date date, String pattern) {
		if (date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 格式化日期
	 * @param date 日期
	 * @param pattern 格式
	 * @return 日期字符串
	 */
	public static String formatDate(Date date, String pattern) {
		if (date == null)
			return null;
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		return df.format(date);
	}
	
	/**
	 * 格式化日期 格式为"yyyy-MM-dd"
	 * @param date 日期
	 * @return 日期字符串
	 */
	public static String formatDate(Date date) {
		return formatDate(date, "yyyy-MM-dd");
	}

    /**
     * 格式化日期 格式为"yyyyMMdd"
     * @param date 日期
     * @return 日期字符串
     */
    public static String formatDate2(Date date) {
        return formatDate(date, "yyyyMMdd");
    }
	
	/**
	 * 把字符串类型的日期转换成Date类型
	 * @param strDate
	 * @return
	 */
	public static Date parseDate(String strDate) {
		return parseDate(strDate, "yyyy-MM-dd");
	}
	
	/**
	 * 把字符串类型的日期转换成Date类型
	 * @param strDate
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(String strDate, String pattern) {
		if (StringUtils.isEmpty(strDate))
			return null;
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = df.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
		return date;
	}
	
	/**
	 * 加减时间
	 * @param strDate 原始时间字符串
	 * @param time 增加/减少的时间数
	 * @param fromPat 原始时间的转换格式
	 * @param toPat 结果时间的转换格式
	 * @param timePat 加减的时间域:秒,分,时,日等,从Calendar中获得
	 * @return
	 */
	public static String addTime(String strDate, int time, String fromPat, String toPat, Integer timePat) {
		if (StringUtils.isEmpty(strDate))
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(parseDate(strDate, fromPat));
		cal.add(timePat, time);
		return formatDate(cal.getTime(), toPat);
	}
	
	/**
	 * 加减时间
	 * @param strDate 原始时间字符串
	 * @param time 增加/减少的时间数
	 * @param fromPat 原始时间的转换格式
	 * @param timePat 加减的时间域:秒,分,时,日等,从Calendar中获得
	 * @return
	 */
	public static Date addTime(String strDate, int time, String fromPat, Integer timePat) {
		if (StringUtils.isEmpty(strDate))
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(parseDate(strDate, fromPat));
		cal.add(timePat, time);
		return cal.getTime();
	}
	
	/**
	 * 加减时间
	 * @param time 增加/减少的时间数
	 * @param timePat 加减的时间域:秒,分,时,日等,从Calendar中获得
	 * @return
	 */
	public static Date addTime(Date date, int time, Integer timePat) {
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(timePat, time);
		return cal.getTime();
	}
	
	/**
	 * 加减时间
	 * @param time 增加/减少的时间数
	 * @param timePat 加减的时间域:秒,分,时,日等,从Calendar中获得
	 * @return
	 */
	public static String addTime(Date date, int time, String toPat, Integer timePat) {
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(timePat, time);
		return formatDate(cal.getTime(), toPat);
	}

    /**
     * 计算年龄
     * @author zhangyi
     * @param birthDay 生日
     * @return
     */

	public static Integer getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();
		
		if (cal.before(birthDay)) {
			return null;
		}

        if(birthDay == null){
            return null;
        }
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;// 注意此处，如果不加1的话计算结果是错误的
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);
		
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		
		int age = yearNow - yearBirth;
		
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else if (monthNow < monthBirth) {
					age--;
				}
		}
		
		return age;
	}
	public static String getStartDate(String startDate){
		return formatDate(parseDate(startDate, "yyyy-MM-dd"), "yyyy-MM-dd HH:mm:ss");
	}
	public static String getEndDate(String endDate){
		return addTime(endDate, 86399, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", Calendar.SECOND);
	}
	public static String getLastDate(String endDate){
		String addTime = addTime(endDate, 1, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", Calendar.DATE);
		return addTime(addTime, -1, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", Calendar.SECOND);
	}

	/**
	 * 根据格式、获取系统时间
	 * @return
	 */
	public static String getSystemDate(String pattern) {
		try {
			Calendar c = Calendar.getInstance();
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			c.setTime(new Date());
			Date d = c.getTime();
			return format.format(d);
		}catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 获取当前时间之前day天的日期
	 * @param day
	 * @return
	 */
	public static String getBeforeDate(int day) {
		try {
			Calendar c = Calendar.getInstance();
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			//过去七天
			c.setTime(new Date());
			c.add(Calendar.DATE, -day);
			Date d = c.getTime();
			return format.format(d);
		}catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 把字符串类型的日期转换成Date类型、在转成字符串返回
	 * @param strDate
	 * @param pattern1
	 * pattern2
	 * @return
	 */
	public static String parseDate(String strDate, String pattern1, String pattern2) {
		if (StringUtils.isEmpty(strDate))
			return null;
		SimpleDateFormat df = new SimpleDateFormat(pattern1);
		Date date = null;
		try {
			date = df.parse(strDate);
			return parseDateToStr(date, pattern2);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获得当月起始时间
	 * @return
	 */
	public static String getStartMounth(String dicomStudyTime) throws Exception {
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
		Date date =sdf.parse(dicomStudyTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		String dateStr = sdf.format(calendar.getTime());
		return dateStr;
	}
	/**
	 * 获得当月结束时间
	 * @return
	 */
	public static String getEndMounth(String dicomStudyTime) throws Exception {
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
		Date date =sdf.parse(dicomStudyTime);
		Calendar endMounth = Calendar.getInstance();
		endMounth.setTime(date);
		endMounth.set(Calendar.DAY_OF_MONTH, endMounth.getActualMaximum(endMounth.DAY_OF_MONTH));
		String dateStr = sdf.format(endMounth.getTime());
		return dateStr;
	}

	/**
	 * 获取当年起始时间
	 */
	public static String getStartYear(String dicomStudyTime) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(dicomStudyTime);
		Calendar today = Calendar.getInstance();
		today.setTime(date);
		today.set(Calendar.MONTH, 0);
		today.set(Calendar.DAY_OF_MONTH, today.getActualMinimum(Calendar.DAY_OF_MONTH));
		String dateStr = sdf.format(today.getTime());
		return dateStr;
	}
	/**
	 * 获取当年结束时间
	 */
	public static String getEndYear(String dicomStudyTime) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(dicomStudyTime);
		Calendar today = Calendar.getInstance();
		today.setTime(date);
		today.set(Calendar.MONTH, 11);
		today.set(Calendar.DAY_OF_MONTH, today.getMaximum(Calendar.DAY_OF_MONTH));
		String dateStr = sdf.format(today.getTime());
		return dateStr;
	}
}
